package pe.edu.upc.e41a.javasoft.domain;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "visita")
/*
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "visita")
@RooDbManaged(automaticallyDelete = true)
*/
public class Visita {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "codigo", columnDefinition = "INT")
    private Integer codigo;

	public Integer getCodigo() {
        return this.codigo;
    }

	public void setCodigo(Integer id) {
        this.codigo = id;
    }

	@ManyToOne
    @JoinColumn(name = "publicacion", referencedColumnName = "codigo", nullable = false)
    private Publicacion publicacion;

	@Column(name = "fecha_registro", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date fechaRegistro;

	public Publicacion getPublicacion() {
        return publicacion;
    }

	public void setPublicacion(Publicacion publicacion) {
        this.publicacion = publicacion;
    }

	public Date getFechaRegistro() {
        return fechaRegistro;
    }

	public void setFechaRegistro(Date fechaRegistro) {
        this.fechaRegistro = fechaRegistro;
    }

	/*
	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Visita().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countVisitas() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Visita o", Long.class).getSingleResult();
    }

	public static List<Visita> findAllVisitas() {
        return entityManager().createQuery("SELECT o FROM Visita o", Visita.class).getResultList();
    }

	public static Visita findVisita(Integer codigo) {
        if (codigo == null) return null;
        return entityManager().find(Visita.class, codigo);
    }

	public static List<Visita> findVisitaEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Visita o", Visita.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Visita attached = Visita.findVisita(this.codigo);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Visita merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Visita merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
    */
}
